﻿function classifyList(id) {
  layer.msg('功能要自己写');
}

layui.use(function() {
  var $ = layui.$;
  var form = layui.form;
  var layer = layui.layer;
  let path_list = location.pathname.split('/');
  let article_id = path_list[path_list.length - 1];
  form.on('submit(comment_submit)', function(data) {
    var field = data.field;
    var elem = data.elem;
    var elemForm = data.form;
    $.ajax({
      url: '/article/article_comment',
      data: JSON.stringify(field),
      type: 'POST',
      dataType: 'text',
      headers: {
        'Content-Type': 'application/json',
        'X-CSRF-TOKEN': getCookie('csrf_access_token'),
      },
      success: function(res) {
        if (!res.code) {
          layer.msg('发布成功', {icon: 1}, function() {
            setTimeout(function() {
              window.location.reload();
            }, 500);
          });
        } else {
          layer.msg(data.message, {icon: 0, time: 2000});
        }
      },
    });
    // …
    return false; // 阻止默认 form 跳转
  });
  $('.collection,.collected').click(async function() {
    // 接口：/article_collection
    // article_id, action
    let action = 'collect';
    if ($(this).hasClass('collected')) {
      action = 'cancel_collect';
    }
    let data = {
      article_id: article_id,
      action: action,
    };
    let ret = await api_post('/article/article_collection', data);
    if (!ret.code) {
      layer.msg(ret.message, {icon: 1});
      setTimeout(function() {
        location.reload();
      }, 1500);

    } else {
      console.log(ret);
    }
  });
  $('.over').hide();
  $('.collected ,.focused').mouseenter(function() {
    $(this).children('.out').hide();
    $(this).children('.over').show();
  });
  $('.collected, .focused').mouseleave(function() {
    $(this).children('.out').show();
    $(this).children('.over').hide();
  });

  // 显示回复框
  $('.comment_reply').click(function() {
    $(this).
        parents('.comment-details').
        siblings('.reply_form').
        show();
  });

  //取消回复框
  $('.reply_cancel').click(function() {
    $(this).parents('.reply_form').hide();
  });

  //评论回复
  $('.reply_form').submit(function() {  // submit 不能用 async
    // 阻止表单的提交;
    let comment = $(this).find('textarea').val();
    let pid = $(this).parents('.comment').attr('data-comment-id');
    let data = {
      article_id: article_id,
      comment: comment,
      parent_id: pid,  // 父级 id
    };
    let response = api_post('/article/article_comment', data);
    response.then(function(ret) {
      if (!ret.code) {
        layer.msg(ret.message, {icon: 1});
        setTimeout(function() {
          location.reload();
        }, 1000);
      }
    });
    return false;
  });

  // 评论点赞
  $('.comment_up').click(async function() {
    let pid = $(this).parents('.comment').attr('data-comment-id');
    let action = 'add';
    if ($(this).hasClass('has_comment_up')) {
      action = 'remove';
    }
    let data = {
      action: action,
      comment_id: pid,
    };
    let ret = await api_post('/article/comment_like', data);
    if (!ret.code) {
      layer.msg(ret.message, {icon: 1});
      setTimeout(function() {
        location.reload();
      }, 900);
    }
    return false;
  });

  // 关注
  $('.focus, .focused').click(async function() {
    let action;
    if ($(this).hasClass('focus')) {
      action = 'follow';
    } else {
      action = 'unfollow';
    }
    let user_id = $(this).attr('data-userid');
    let data = {
      user_id: user_id,
      action: action,
    };
    let ret = await api_post('/article/followed_user', data);
    // 如果关注成功
    if (!ret.code) {
      layer.msg(ret.message, {icon: 1, time: 1000}, function() {
        setTimeout(function() {
          location.reload();
        }, 2000);
      });
      if ($(this).hasClass('focused')) {
        $(this).hide().siblings('.focus').show();
      } else {
        $(this).hide().siblings('.focused').show();
      }
    }
    return false;
  });

});

function unlogin() {
  layer.msg('登陆后再操作哦~~~~');
}